package com.shangguan.atcrowdfunding.manager.controller;

import com.shangguan.atcrowdfunding.bean.User;
import com.shangguan.atcrowdfunding.manager.service.UserService;
import com.shangguan.atcrowdfunding.utils.AjaxResult;
import com.shangguan.atcrowdfunding.utils.DateUtil;
import com.shangguan.atcrowdfunding.utils.MD5Util;
import com.shangguan.atcrowdfunding.utils.Page;
import com.shangguan.atcrowdfunding.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserController
 * @Description:
 * @Author: 一尘
 * @Date: 2020 年 01 月 14 11:08
 * @Version 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService  userService;

    @RequestMapping("/toIndex")
    public String toIndex(){
        return "user/index";
    }

    @RequestMapping("/toUserAdd")
    public String toUserAdd(){
        return "user/add";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id,Model model){
        System.out.println("id-----------"+id);
     User user =    userService.selectByPrimaryKey(Integer.parseInt(id));
     model.addAttribute("user",user);
        return "user/edit";
    }
    @ResponseBody
    @RequestMapping("/doUpdate")
    public   Object doUpdate(String id,String loginacct,String username,String email){
        AjaxResult  result = new AjaxResult();
        try {
            User user = new User();
            user.setId(Integer.parseInt(id));
            user.setLoginacct(loginacct);
            user.setUsername(username);
            user.setEmail(email);
            user.setUserpswd(MD5Util.digest("123"));
            user.setCreatetime(DateUtil.getSystemDate());
            userService.updateByPrimaryKey(user);
            result.setSuccess(true);
            result.setMessage("用户信息修改成功!");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("用户信息修改失败!");
        }
        return   result;
    }

    @ResponseBody
    @RequestMapping("/doUserAdd")
    public Object doUserAdd(String loginacct,String username,String email){
        AjaxResult  result = new AjaxResult();
        try {
            User user = new User();
            user.setLoginacct(loginacct);
            user.setUsername(username);
            user.setEmail(email);
            user.setUserpswd(MD5Util.digest("123"));
            user.setCreatetime(DateUtil.getSystemDate());
            userService.insert(user);
            result.setSuccess(true);
            result.setMessage("用户信息添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("用户信息添加失败!");
        }

        return result;
    }



    // 分页的模糊查询异步操作
    @ResponseBody
    @RequestMapping("/userPage")
    public Object userPage(@RequestParam(defaultValue = "1") String currentpage,
                           @RequestParam(value = "queryText",required = false) String queryText){
        //System.out.println("queryText----"+queryText);

        AjaxResult result = new AjaxResult();

        try {


            Map<String,Object>  paramMap = new HashMap();

                /*if(queryText !=null && queryText.equals("")){
                    paramMap.put("queryText",queryText);
                }*/

            if(StringUtil.isNotEmpty(queryText)){
                paramMap.put("queryText",queryText);
            }
            // 分页查询需要两个数据  当前页  总记录数
            List<User> userList = userService.selectLikeAll(paramMap);

            //System.out.println("currentpage-------"+currentpage);
            Page page = new Page(Integer.parseInt(currentpage),userList.size());
           // System.out.println("page"+page);
                paramMap.put("startIndex",page.getStartIndex());
                paramMap.put("size",page.getPageSize());

            //List<User>   userPage =  userService.queryUserByPage(page.getStartIndex(),page.getPageSize());
            List<User>   userPage =  userService.queryUserByPage(paramMap);
            result.setSuccess(true);
            result.setMessage("数据查询成功!");
            result.setPage(page);
            result.setUserList(userPage);

        } catch (NumberFormatException e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("用户数据查询失败!");
        }


        return result ;
    }

/*
    // 分页的异步操作
    @ResponseBody
    @RequestMapping("/userPage")
    public Object userPage(@RequestParam(defaultValue = "1") String currentpage,String queryText){
        System.out.println("queryText----"+queryText);

        AjaxResult  result = new AjaxResult();

        try {
            // 分页查询需要两个数据  当前页  总记录数
            List<User> userList = userService.selectAll();
            System.out.println("currentpage-------"+currentpage);
            Page page = new Page(Integer.parseInt(currentpage),userList.size());
            System.out.println("page"+page);
            List<User>   userPage =  userService.queryUserByPage(page.getStartIndex(),page.getPageSize());
            result.setSuccess(true);
            result.setMessage("数据查询成功!");
            result.setPage(page);
            result.setUserList(userPage);

        } catch (NumberFormatException e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("用户数据查询失败!");
        }


        return result ;
    }*/

   /* // 分页的同步操作
   @RequestMapping("userPage")
    public String userPage(@RequestParam(defaultValue = "1") String currentpage, Model  model){
    // 分页查询需要两个数据  当前页  总记录数
     List<User> userList = userService.selectAll();
        System.out.println("currentpage-------"+currentpage);
        Page page = new Page(Integer.parseInt(currentpage),userList.size());
        System.out.println("page"+page);
        List<User>   userPage =  userService.queryUserByPage(page.getStartIndex(),page.getPageSize());

        model.addAttribute("userPage",userPage);

         // page 就是分页的核心对象
        model.addAttribute("page",page);

        return "user/index";
    }*/






    @RequestMapping("/list")
    public String selectAll(Model model){
    List<User> userList =  userService.selectAll();

    model.addAttribute("userList",userList);
    return "UsersList";
    }




}
